package com.sky.controller.admin;

import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.properties.JwtProperties;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.CategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/category")
@RequiredArgsConstructor
@Slf4j
@Api(tags = "分类控制器")
public class CategoryController {

    private final CategoryService categoryService;

    private final JwtProperties jwtProperties;

    @ApiOperation("分类分页")
    @GetMapping("/page")
    public Result<PageResult> page(CategoryPageQueryDTO categoryPageQueryDTO){
        PageResult pageResult = categoryService.selectCategoryByName(categoryPageQueryDTO);
        return Result.success(pageResult);
    }

    @ApiOperation("分类新增")
    @PostMapping
    public Result<?> create(@RequestBody CategoryDTO categoryDTO){
        categoryService.create(categoryDTO);
        return Result.success();
    }

    @ApiOperation("分类删除")
    @DeleteMapping
    public Result<?> delete(Long id){
        categoryService.deleteCategoryById(id);
        return Result.success();
    }

    @ApiOperation("分类修改")
    @PutMapping
    public Result<?> update(@RequestBody CategoryDTO categoryDTO){
        categoryService.updateCategory(categoryDTO);
        return Result.success();
    }

    @ApiOperation("分类修改状态")
    @PostMapping("/status/{status}")
    public Result<?> updateStatusById(Long id,@PathVariable Integer status){
        categoryService.updateCategoryStatus(id,status);
        return Result.success();
    }

    @ApiOperation("根据类型查询分类")
    @GetMapping("/list")
    public Result list(Integer type){
        List<Category> categoryList = categoryService.selectCategoryByType(type);
        return Result.success(categoryList);
    }
}
